package gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.JComboBox;

import controllers.AdminUIP;
import controllers.OBLSettings;

import javax.swing.JRadioButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.ImageIcon;

/**
 * This class represents the OBL system administrator Gui
 * Date: 21/12/2012
 * @author Alon Shahaf
 *
 */
public class AdminUI extends JPanel 
{
	private static final long serialVersionUID = 1L;
	
	private AdminUIP auip = null;
	private JLabel lblOblSystemAdministrator = null;
	private JLabel lblUnlocksASpecified = null;
	private JLabel lblCreateALibrarian = null;
	private JSeparator separator = null;
	JButton btnLogout = null;
	JButton btnUnlockAUser = null;
	JButton btnExit = null;
	JButton btnCreateLibrarian = null;
	private JTextField txtLibID;
	private JTextField txtLastName;
	private JTextField txtEmail;
	private JTextField txtFirstName;
	private JRadioButton rdbtnNo,rdbtnYes;
	private JTextField txtID;
	private JLabel backgroundlabel;

	/**
	 * Constructor for the Admin GUI with initialization.
	 */
	public AdminUI() 
	{
		super();
		this.auip = new AdminUIP();
		initialize();
	}
	
	/**
	 * Initializes the GUI
	 */	
	private void initialize()
	{
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		setLayout(null);
		
		lblOblSystemAdministrator = new JLabel("OBL System Administrator");
		lblOblSystemAdministrator.setFont(OBLSettings.headlinesTextFont);
		lblOblSystemAdministrator.setBounds(273, 14, 299, 31);
		add(lblOblSystemAdministrator);
		
		btnLogout = new JButton("Logout");
		btnLogout.setFont(OBLSettings.buttonTextFont);
		btnLogout.setBounds(620, 11, 83, 41);
		add(btnLogout);
		
		JPanel unlockPanel = new JPanel();
		unlockPanel.setOpaque(false);
		unlockPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Unlock a user", TitledBorder.LEADING, TitledBorder.TOP, OBLSettings.titlesTextFont, OBLSettings.titlesTextColor));
		unlockPanel.setBounds(10, 63, 780, 124);
		add(unlockPanel);
		unlockPanel.setLayout(null);
		
		btnUnlockAUser = new JButton("Unlock");
		btnUnlockAUser.setBounds(628, 70, 130, 41);
		unlockPanel.add(btnUnlockAUser);
		btnUnlockAUser.setFont(OBLSettings.buttonTextFont);
		
		lblUnlocksASpecified = new JLabel("<html><body>\r\nThis operation will unlock a user and allow him to login.\r\n<br>\r\nMake sure the user is allowed to use the system before unlocking!\r\n</body></html>");
		lblUnlocksASpecified.setBounds(10, 28, 366, 83);
		unlockPanel.add(lblUnlocksASpecified);
		lblUnlocksASpecified.setFont(OBLSettings.labelFont);
		
		JLabel lblSelectA = new JLabel("1. Select a user:");
		lblSelectA.setFont(OBLSettings.labelFont);
		lblSelectA.setBounds(439, 28, 128, 14);
		unlockPanel.add(lblSelectA);
		
		final JComboBox<String> comboBoxLockedUsers = new JComboBox<String>();
		comboBoxLockedUsers.setBounds(628, 27, 119, 23);
		comboBoxLockedUsers.setFont(OBLSettings.textFont);
		// Load locked users to combo box
		ArrayList<String> lockedUsers = auip.getLockedUsers();
		for(String item: lockedUsers)
			comboBoxLockedUsers.addItem(item);


		if (lockedUsers.isEmpty()) btnUnlockAUser.setEnabled(false);
		unlockPanel.add(comboBoxLockedUsers);
		
		JLabel lblClickTo = new JLabel("2. Click to unlock:");
		lblClickTo.setFont(OBLSettings.labelFont);
		lblClickTo.setBounds(439, 80, 128, 20);
		unlockPanel.add(lblClickTo);
		
		JPanel createLibrarianPanel = new JPanel();
		createLibrarianPanel.setOpaque(false);
		createLibrarianPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Create librarian", TitledBorder.LEADING, TitledBorder.TOP, OBLSettings.titlesTextFont, OBLSettings.titlesTextColor));
		createLibrarianPanel.setBounds(10, 196, 780, 238);
		add(createLibrarianPanel);
		createLibrarianPanel.setLayout(null);
		
		btnCreateLibrarian = new JButton("Create librarian");
		btnCreateLibrarian.setBounds(627, 186, 131, 41);
		createLibrarianPanel.add(btnCreateLibrarian);
		btnCreateLibrarian.setFont(OBLSettings.buttonTextFont);
		
		lblCreateALibrarian = new JLabel("<html><body>\r\nCreate a new librarian user for the system.\r\n<br>\r\n<br>\r\n<b>Note:</b>\r\n<br>\r\nThis function is mostly used when the DB is empty and a librarian user is first entered.\r\n</body></html>");
		lblCreateALibrarian.setBounds(10, 43, 361, 170);
		createLibrarianPanel.add(lblCreateALibrarian);
		lblCreateALibrarian.setFont(OBLSettings.labelFont);
		
		JLabel lblEnterLibrarian = new JLabel("1. Enter librarian id:");
		lblEnterLibrarian.setFont(OBLSettings.labelFont);
		lblEnterLibrarian.setBounds(442, 30, 165, 14);
		createLibrarianPanel.add(lblEnterLibrarian);
		
		txtLibID = new JTextField();
		txtLibID.setBounds(627, 28, 119, 23);
		txtLibID.setFont(OBLSettings.textFont);
		createLibrarianPanel.add(txtLibID);
		txtLibID.setColumns(10);
		
		JLabel lblClickTo_1 = new JLabel("6. Click to create:");
		lblClickTo_1.setFont(OBLSettings.labelFont);
		lblClickTo_1.setBounds(442, 199, 165, 14);
		createLibrarianPanel.add(lblClickTo_1);
		
		txtFirstName = new JTextField();
		txtFirstName.setBounds(628, 59, 118, 23);
		txtFirstName.setFont(OBLSettings.textFont);
		createLibrarianPanel.add(txtFirstName);
		txtFirstName.setColumns(10);
		
		txtLastName = new JTextField();
		txtLastName.setBounds(628, 93, 118, 23);
		txtLastName.setFont(OBLSettings.textFont);
		createLibrarianPanel.add(txtLastName);
		txtLastName.setColumns(10);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(627, 124, 119, 23);
		txtEmail.setFont(OBLSettings.textFont);
		createLibrarianPanel.add(txtEmail);
		txtEmail.setColumns(10);
		
		JLabel lblFirstName = new JLabel("2. Enter First name:");
		lblFirstName.setBounds(442, 61, 165, 14);
		lblFirstName.setFont(OBLSettings.labelFont);
		createLibrarianPanel.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("3. Enter Last name:");
		lblLastName.setBounds(442, 95, 165, 14);
		lblLastName.setFont(OBLSettings.labelFont);
		createLibrarianPanel.add(lblLastName);
		
		JLabel lblEmail = new JLabel("4. Enter Email:");
		lblEmail.setBounds(442, 126, 165, 14);
		lblEmail.setFont(OBLSettings.labelFont);
		createLibrarianPanel.add(lblEmail);
		
		rdbtnYes = new JRadioButton("Yes");
		rdbtnYes.setOpaque(false);
		rdbtnYes.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				rdbtnNo.setSelected(false);
			}
		});
		rdbtnYes.setBounds(628, 151, 59, 23);
		rdbtnYes.setFont(OBLSettings.labelFont);
		rdbtnYes.setSelected(true);
		createLibrarianPanel.add(rdbtnYes);
		
		rdbtnNo = new JRadioButton("No");
		rdbtnNo.setOpaque(false);
		rdbtnNo.setBounds(696, 151, 49, 23);
		rdbtnNo.setFont(OBLSettings.labelFont);
		rdbtnNo.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				rdbtnYes.setSelected(false);
			}
		});		
		createLibrarianPanel.add(rdbtnNo);
		
		JLabel lblLibrarianManager = new JLabel("5. Librarian Manager?");
		lblLibrarianManager.setBounds(442, 155, 150, 19);
		lblLibrarianManager.setFont(OBLSettings.labelFont);
		lblEmail.setFont(OBLSettings.labelFont);
		createLibrarianPanel.add(lblLibrarianManager);
		
		separator = new JSeparator();
		separator.setBounds(0, 57, 835, 30);
		add(separator);
		
		btnExit = new JButton("Exit");
		btnExit.setFont(OBLSettings.buttonTextFont);
		btnExit.setBounds(707, 11, 83, 41);
		add(btnExit);
		
		JPanel graduatedPanel = new JPanel();
		graduatedPanel.setOpaque(false);
		graduatedPanel.setLayout(null);
		graduatedPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Mark user as graduated", TitledBorder.LEADING, TitledBorder.TOP, OBLSettings.titlesTextFont, OBLSettings.titlesTextColor));
		graduatedPanel.setBounds(10, 446, 780, 124);
		add(graduatedPanel);
		
		JButton btnMarkGraduated = new JButton("Graduated");
		btnMarkGraduated.setFont(OBLSettings.buttonTextFont);
		btnMarkGraduated.setBounds(628, 70, 130, 41);
		graduatedPanel.add(btnMarkGraduated);
		
		JLabel lblThisOperationIllustrates = new JLabel("<html><body>\r\nThis operation illustrates an external system that sends messages regarding users graduation.\r\n<br>\r\nIt is used to trigger the 'graduated user event' on the OBL system.\r\n</body></html>");
		lblThisOperationIllustrates.setFont(OBLSettings.labelFont);
		lblThisOperationIllustrates.setBounds(10, 28, 366, 83);
		graduatedPanel.add(lblThisOperationIllustrates);
		
		JLabel lblEnterUser = new JLabel("1. Enter user ID:");
		lblEnterUser.setFont(OBLSettings.labelFont);
		lblEnterUser.setBounds(439, 28, 128, 14);
		graduatedPanel.add(lblEnterUser);
		
		JLabel lblClickTo_2 = new JLabel("2. Click to mark graduated:");
		lblClickTo_2.setFont(OBLSettings.labelFont);
		lblClickTo_2.setBounds(439, 74, 179, 33);
		graduatedPanel.add(lblClickTo_2);
		
		txtID = new JTextField();
		txtID.setFont(OBLSettings.textFont);
		txtID.setColumns(10);
		txtID.setBounds(628, 28, 119, 23);
		graduatedPanel.add(txtID);
		
	    backgroundlabel = new JLabel("");
	    backgroundlabel.setIcon(new ImageIcon(""));
		backgroundlabel.setBounds(0, 57, 800, 600);
		add(backgroundlabel);
	    
		
		// Buttons listeners
		btnUnlockAUser.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if (comboBoxLockedUsers.getItemCount() == 0)
				{
					JOptionPane.showMessageDialog( WelcomeUI.wui, "No user selected!","Unlock User",JOptionPane.ERROR_MESSAGE);
					return;
				}
				int unlock_res = auip.unlockUser(comboBoxLockedUsers.getSelectedItem().toString());
				switch(unlock_res)
				{
					case 1:
					{
						// Remove user from list
						comboBoxLockedUsers.removeItem(comboBoxLockedUsers.getSelectedItem().toString());
						if (comboBoxLockedUsers.getItemCount() == 0)
							btnUnlockAUser.setEnabled(false);
						JOptionPane.showMessageDialog( WelcomeUI.wui, "User unlocked!","Unlock User",JOptionPane.INFORMATION_MESSAGE);
						break;
					}
					case -99:
					{
						JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Unlock User",JOptionPane.ERROR_MESSAGE);
						break;
					}
					default:
					{
						JOptionPane.showMessageDialog( WelcomeUI.wui, "User already unlocked!","Unlock User",JOptionPane.ERROR_MESSAGE);
						break;
					}
				} // End switch
			}
		 });
		
		btnCreateLibrarian.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				System.out.println("Create librarian button clicked");
				if (getLibID().isEmpty() || getLibFName().isEmpty() || getLibLName().isEmpty() || getLibEmail().isEmpty())
				{
					JOptionPane.showMessageDialog( WelcomeUI.wui, "Please fill in all fields!","Create a Librarian",JOptionPane.ERROR_MESSAGE);
					if (getLibID().isEmpty()) txtLibID.grabFocus();
					else if (getLibFName().isEmpty()) txtFirstName.grabFocus();
					else if (getLibLName().isEmpty()) txtLastName.grabFocus();
					else if (getLibEmail().isEmpty()) txtEmail.grabFocus();
				}
				else
				{
					int create_res = auip.createLibrarian(getLibID(),getLibFName(),getLibLName(),getLibEmail(),getLibManager());
					switch(create_res)
					{
						case 1: // User created successfully
						{
							txtLibID.setText(null);
							txtFirstName.setText(null);
							txtLastName.setText(null);
							txtEmail.setText(null);
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User created successfully! (default password=12345)","Create Librarian",JOptionPane.INFORMATION_MESSAGE);
							break;
						} 
						case -1: // User already exists
						{
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User already exists!","Create Librarian",JOptionPane.ERROR_MESSAGE);
							break;
						}
						default: // Error
						{
							JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Create Librarian",JOptionPane.ERROR_MESSAGE);
							break;
						}
					} // End switch
				}
			}
		});
		btnMarkGraduated.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				System.out.println("Graduate button clicked");
				if (getUserID().isEmpty())
				{
					JOptionPane.showMessageDialog( WelcomeUI.wui, "Please fill in User ID fields","Mark user as graduated",JOptionPane.ERROR_MESSAGE);
					txtID.grabFocus();
				}
				else
				{
					int graduate_res = auip.graduateUser(getUserID());
					switch(graduate_res)
					{
						case 2: // User graduated successfully
						{
							txtID.grabFocus();
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User graduated successfully!","Mark user as graduated",JOptionPane.INFORMATION_MESSAGE);
							break;
						}
						case 1: // User graduated successfully and was forced to logout
						{
							txtID.grabFocus();
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User graduated successfully (forced to logout)!","Mark user as graduated",JOptionPane.INFORMATION_MESSAGE);
							break;
						} 
						case 0: // User is suspended and was forced to logout
						{
							txtID.grabFocus();
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User is now suspended - active borrow(s) exists (forced to logout)!","Mark user as graduated",JOptionPane.INFORMATION_MESSAGE);
							break;
						}
						case -1: // User cannot be graduated
						{
							txtID.grabFocus();
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User cannot graduate - not a subscriber!","Mark user as graduated",JOptionPane.ERROR_MESSAGE);
							break;
						}
						case -2: // No such user!
						{
							txtID.grabFocus();
							JOptionPane.showMessageDialog( WelcomeUI.wui, "No such user!","Mark user as graduated",JOptionPane.ERROR_MESSAGE);
							break;
						}
						case -3: // User is suspended
						{
							txtID.grabFocus();
							JOptionPane.showMessageDialog( WelcomeUI.wui, "User is now suspended - active borrow(s) exists!","Mark user as graduated",JOptionPane.INFORMATION_MESSAGE);
							break;
						}						
						default: // Error
						{
							JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Mark user as graduated",JOptionPane.ERROR_MESSAGE);
							break;
						}
					} // End switch
				}
			}
		});
	}
	
	/**
	 * Get the librarian ID.
	 * @return Librarian ID.
	 */
	public String getLibID()
	{
		return this.txtLibID.getText();
	}

	/**
	 * Get the librarian first name.
	 * @return Librarian first name.
	 */
	public String getLibFName()
	{
		return this.txtFirstName.getText();
	}

	/**
	 * Get the librarian last name.
	 * @return Librarian last name.
	 */
	public String getLibLName()
	{
		return this.txtLastName.getText();
	}
	
	/**
	 * Get the librarian email.
	 * @return Librarian email.
	 */
	public String getLibEmail()
	{
		return this.txtEmail.getText();
	}
	
	/**
	 * Returns selection value of the Manager radio button.
	 * @return true	if Librarian manager is selected, false otherwise.
	 */
	public boolean getLibManager()
	{
		if (this.rdbtnYes.isSelected()) return true;
		return false;
	}
	
	/**
	 * Get the user ID.
	 * @return User ID.
	 */
	public String getUserID()
	{
		return this.txtID.getText();
	}
	
	/**
	 * Prints the backgrounds pictures.
	 */
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		g.drawImage((OBLSettings.IMG_ADMIN_BACKGROUND).getImage(), 0, 57, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		//g.drawImage((new ImageIcon("D:/4.jpg")).getImage(), 0, 0,OBLSettings.pane2Size[0],OBLSettings.pane2Size[1], null);
	}
}
